﻿using log4net;
using Mate.Caching;
using Mate.Comment.Api.Controller.Models.Comment;
using Mate.Comment.Service;
using Mate.Core.Base;
using Mate.Core.Configuration;
using Mate.Db.Model.Comment;
using Mate.Dto.Comment;
using Mate.Framework.BaseController.Api;
using Mate.Framework.Extensions;
using Mate.Merchant.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mate.Comment.Api.Controller.Controller
{
    public class CommentController: CommentBaseController
    {
        private readonly ILog log;
        private readonly ICacheManager cache;
        private readonly MateConfig mateConfig;
        private readonly ICommentService commentService;
        private readonly ILikeService likeService;
        private readonly IMerchantCategoryService merchantCategoryService;

        public CommentController(ILog log, ICacheManager cache, MateConfig mateConfig, ICommentService commentService, ILikeService likeService,
            IMerchantCategoryService merchantCategoryService)
        {
            this.log = log;
            this.cache = cache;
            this.mateConfig = mateConfig;
            this.commentService = commentService;
            this.likeService = likeService;
            this.merchantCategoryService = merchantCategoryService;
        }

        #region 根据商户分类获取点评列表
        /// <summary>
        /// 根据商户分类获取点评列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BasePageResult<IList<HomeCommentModel>> HomeList(HomeCommentRequest req)
        {
            BasePageResult<IList<HomeCommentModel>> result = new BasePageResult<IList<HomeCommentModel>>();
            if (req.merchant_category_id < 1)
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "参数错误";
                return result;
            }
            int totalCount = 0;
            var merchantcategory = merchantCategoryService.GetCurrentMerchantCategoryAndChildren(req.merchant_category_id).Select(x => x.id);
            var dataList = commentService.GetHomeCommentList(req.PageIndex, req.PageSize, merchantcategory, ref totalCount);
            if (dataList == null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "暂无相关数据";
                return result;
            }
            result.PageSize = req.PageSize;
            result.PageIndex = req.PageIndex;
            result.TotalCount = totalCount;
            result.ResultCode = ResultCode.Success;
            result.Message = "OK";
            result.Data = dataList;

            return result;
        }
        #endregion

        #region 首页点评点赞和取消点赞
        /// <summary>
        /// 首页点评点赞和取消点赞
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResult HomeLike(HomeLikeRequest req)
        {
            BaseResult result = new BaseResult();
            if (req.comment_id < 1)
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "参数错误";
                return result;
            }
            var currentUser = CurrentUser(req.token);
            if (currentUser == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "获取登录信息失败";
                return result;
            }

            if (req.is_like == 1)
            {
                likeService.Insert(new Db.Model.Comment.mp_like()
                {
                    createtime = DateTime.Now,
                    is_audit = 1,
                    is_delete = 1,
                    liketype = 1,
                    relateid = req.comment_id,
                    userid = currentUser.userid
                });
            }
            else
            {
                likeService.Delete(x => x.liketype == 1 && x.userid == currentUser.userid && x.relateid == req.comment_id);
            }
            result.ResultCode = ResultCode.Success;
            result.Message = "提交成功";
            return result;
        }
        #endregion

        #region 获取商户点评列表
        /// <summary>
        /// 获取商户点评列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BasePageResult<IList<MerchantCommentDto>> MerchantCommentList(MerchantCommentListRequest req)
        {
            BasePageResult<IList<MerchantCommentDto>> result = new BasePageResult<IList<MerchantCommentDto>>();
            if (req.merchantId == 0)
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "参数错误";
                return result;
            }
            int totalCount = 0;
            var dataList = commentService.GetMerchantCommentList(req.PageIndex, req.PageSize, req.merchantId, ref totalCount);
            if (dataList == null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "暂无相关数据";
                return result;
            }
            result.PageSize = req.PageSize;
            result.PageIndex = req.PageIndex;
            result.TotalCount = totalCount;
            result.ResultCode = ResultCode.Success;
            result.Message = "OK";
            result.Data = dataList;

            return result;
        }
        #endregion

        #region 图片上传
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResult ImgUpload(ImgUploadRequest req)
        {
            return mateConfig.QiNiu.QiniuUpload(req.imgBase64, req.fileName);
        }
        #endregion

        #region 添加点评信息
        /// <summary>
        /// 添加点评信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResult AddComment(CreateCommentRequest req)
        {
            BaseResult result = AddCommentVali(req);
            if (result.ResultCode != ResultCode.Success)
            {
                return result;
            }
            var currentUser = CurrentUser(req.token);
            if (currentUser == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "获取登录信息失败";
                return result;
            }
            var comment = new mp_comment()
            {
                userid = currentUser.userid,
                title = req.title,
                content = req.content,
                comment_tag = req.comment_tag,
                comment_type = req.comment_type,
                relateid = req.relateid,
                overallscore = req.overallscore,
                tastescore = req.tastescore,
                enviromentscore = req.enviromentscore,
                servicescore = req.servicescore,
                video_one = req.video_one,
                percost = req.percost,
                is_delete = 1,
                is_anonymous = req.is_anonymous,
                is_audit = 2,
                createtime = DateTime.Now
            };
            if (req.imgs != null && req.imgs.Length > 0)
            {
                for (int i= 0;i < req.imgs.Length;i++)
                {
                    switch (i)
                    {
                        case 0:
                            comment.img_one = req.imgs[0];break;
                        case 1:
                            comment.img_two = req.imgs[1]; break;
                        case 2:
                            comment.img_three = req.imgs[2]; break;
                        case 3:
                            comment.img_four = req.imgs[3]; break;
                        case 4:
                            comment.img_five = req.imgs[4]; break;
                    }
                }
            }
            if (commentService.Insert(comment) > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "添加点评成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "添加点评失败";
            }
            return result;
        }
        [NonAction]
        private BaseResult AddCommentVali(CreateCommentRequest req)
        {
            BaseResult result = new BaseResult();
            if (string.IsNullOrWhiteSpace(req.token))
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "Token不能为空";
                return result;
            }

            if (string.IsNullOrWhiteSpace(req.title))
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "点评标题不能为空";
                return result;
            }
            if (string.IsNullOrWhiteSpace(req.content))
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "点评内容不能为空";
                return result;
            }
            if (req.comment_type!=10&&req.comment_type!=20)
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "点评类型错误";
                return result;
            }

            if (req.relateid<1)
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = $"{(req.comment_type==10?"商户":"商品")}不能为空";
                return result;
            }
            result.ResultCode = ResultCode.Success;
            return result;
        }

        #endregion
    }
}
